const infodata = {
  data: [],
  childrenData: [],
};
const reducer = (state = infodata, { type, payload }: any) => {
  let newState = JSON.parse(JSON.stringify(state));
  switch (type) {
    case "GET_LIST":
      newState.data = payload;
      newState.childrenData = payload;
      return newState;
    case "ADD_LIST":
      newState.data.push(payload);
      return newState;
    case "SUB_LIST":
      newState.data.forEach((item: any, index: number) => {
        if (item.id === payload) {
          newState.data.splice(index, 1);
        }
      });
      return newState;
      case "SEARCH":
       newState.data= newState.childrenData.filter((item:any)=>{
         return item.name.includes(payload)||item.key.toString().includes(payload)
        })
        return newState;
    default:
      return newState;
  }
};
export default reducer;
